package com.example.wanandroid.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.wanandroid.core.bean.main.banner.BannerData;
import com.example.wanandroid.ui.activity.ProjectDetailActivity;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * @author jiangyi
 */
public class BannerImageAdapter extends BannerAdapter<BannerData, BannerImageAdapter.BannerViewHolder> {
    Context context;
    public BannerImageAdapter(Context context, List<BannerData> datas) {
        super(datas);
        this.context = context;
    }

    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(parent.getContext());
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerViewHolder(imageView);
    }

    @Override
    public void onBindView(BannerViewHolder holder, BannerData data, int position, int size) {
        View itemView = holder.itemView;
        String url = data.getUrl();
        itemView.setOnClickListener(v->{
            Intent intent = new Intent(itemView.getContext(), ProjectDetailActivity.class);
            intent.putExtra("link",url);
            itemView.getContext().startActivity(intent);
        });
        Glide.with(holder.itemView.getContext())
                .load(data.getImagePath())
                .into(holder.imageView);

    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BannerViewHolder(@NonNull ImageView view) {
            super(view);
            this.imageView = view;
        }
    }
}
